//
//  TeamModel.swift
//  dataBaseProj
//
//  Created by sea on 2020/1/7.
//  Copyright © 2020 CDSCRB. All rights reserved.
//

import UIKit
@objcMembers
class TeamModel: NSObject {
    /// 队伍id
    var teamID:String?
    
    /// 队名
    var teamName:String?
    
    /// 球员数量
    var teamPlayerCount:NSNumber?
    /// 首发数
    var shoufaCount:NSNumber?
    
    /// 替补数
    var tibuCount:NSNumber?
    /// 图片
    var teamHead:String?
    
    /// 输赢
    var teamWinOrLose:String? //0输 1赢
    
    /// 数据id
    var teamMatchDataId:String?
    
    
    //    var playerCount:Int?{
    //        get{
    //            if !DYStringIsEmpty(value: self.teamID as AnyObject?){
    //                let sql = "select count(*) from \(playerTableName) where \(DHPlayerID) like '\(self.teamID!)_%%'"
    //                var count:Int = 0
    //                DHDataBaseOperate.searchDatasFromDB(sqlString: sql) { (rs) in
    //                    if rs is FMResultSet{
    //                        let result = rs as! FMResultSet
    //                        print(result.int(forColumnIndex: 0))
    //                        count = Int(result.int(forColumnIndex: 0))
    //                    }
    //                }
    //                return count
    //            }else{
    //                return 0
    //            }
    //        }
    //    }
    //    var shoufaCount:Int?{
    //        get{
    //            if !DYStringIsEmpty(value: self.teamID as AnyObject?){
    //                let sql = "select count(*) from \(playerTableName) where \(DHPlayerID) like '\(self.teamID!)_%%'"
    //               var count:Int = 0
    //                DHDataBaseOperate.searchDatasFromDB(sqlString: sql) { (rs) in
    //                    if rs is FMResultSet{
    //                        let result = rs as! FMResultSet
    //                        print(result.int(forColumnIndex: 0))
    //                        count = Int(result.int(forColumnIndex: 0))
    //                    }
    //                }
    //                return count
    //            }else{
    //                return 0
    //            }
    //        }
    //    }
    //
    //    var tibuCount:Int?{
    //           get{
    //            return ((self.playerCount!) + (self.shoufaCount!)*(-1))
    //           }
    //       }
    //    var update:String?//数据更新
    
    var matchInfo:MatchDataModel?{
        get{
            if !DYStringIsEmpty(value: self.teamMatchDataId as AnyObject?) {
                let model:MatchDataModel = MatchDataModel.getMatchDataModelWithID(matchDataID: self.teamMatchDataId!)
                return model
            }else{
                return MatchDataModel()
            }
        }
    }
    
    class func getTeamModelWithTeamID(teamId:String, CompeleteBlock:@escaping compeletBlock){
        let sqlString = "select * from \(teamTableName) where \(DHTeamID) = \"\(teamId)\"  "
        print(sqlString)
        //        var Teamodel:TeamModel?
        DHDataBaseManager.shareDataBaseManager.searchWithSql(sql: sqlString) { (result) in
            if result is FMResultSet{
                let rs = result as! FMResultSet
                while rs.next() {
                    let teamModel = TeamModel()
                    teamModel.teamID = rs.string(forColumn: DHTeamID)
                    teamModel.teamHead = rs.string(forColumn: DHTeamHead)
                    teamModel.teamName = rs.string(forColumn: DHTeamName)
                    teamModel.teamWinOrLose = rs.string(forColumn: DHTeamWinOrLose)
                    teamModel.teamPlayerCount = Int(rs.int(forColumn: DHTeamPlayerCount)) as NSNumber
                    teamModel.shoufaCount = Int(rs.int(forColumn: DHTeamShouFaPlayerCount)) as NSNumber
                    teamModel.tibuCount = Int(rs.int(forColumn: DHTeamTiBuPlayerCount)) as NSNumber
                    teamModel.teamMatchDataId = rs.string(forColumn: DHTeamMatchDataId)
                    CompeleteBlock(teamModel)
                    //                    Teamodel = teamModel
                }
                rs.close()
                CompeleteBlock(NSNumber.init(value: false))
            }else{
                CompeleteBlock(NSNumber.init(value: false))
            }
        }
    }
    
    class func getTeamModelWithTeamID(teamId:String) -> TeamModel{
        let sqlString = "select * from \(teamTableName) where \(DHTeamID) = \"\(teamId)\"  "
        print(sqlString)
        var Teamodel:TeamModel = TeamModel()
        DHDataBaseManager.shareDataBaseManager.searchWithSql(sql: sqlString) { (result) in
            if result is FMResultSet{
                let rs = result as! FMResultSet
                while rs.next() {
                    let teamModel = TeamModel()
                    teamModel.teamID = rs.string(forColumn: DHTeamID)
                    teamModel.teamHead = rs.string(forColumn: DHTeamHead)
                    teamModel.teamName = rs.string(forColumn: DHTeamName)
                    teamModel.teamWinOrLose = rs.string(forColumn: DHTeamWinOrLose)
                    teamModel.teamPlayerCount = Int(rs.int(forColumn: DHTeamPlayerCount)) as NSNumber
                    teamModel.shoufaCount = Int(rs.int(forColumn: DHTeamShouFaPlayerCount)) as NSNumber
                    teamModel.tibuCount = Int(rs.int(forColumn: DHTeamTiBuPlayerCount)) as NSNumber
                    teamModel.teamMatchDataId = rs.string(forColumn: DHTeamMatchDataId)
                    Teamodel = teamModel
                }
                rs.close()
            }
        }
        return Teamodel
    }
    
    class func getTeamPlayerListWihtTeamID(teamID:String)->[PlayerModel]{
        let sql  = "select * from \(playerTableName) where \(DHPlayerID) like '\(teamID)_%%'"
        var dataArr = [PlayerModel]()
        var shoufaArr = [PlayerModel]()
        var tibuArr = [PlayerModel]()
        DHDataBaseOperate.searchDatasFromDB(sqlString: sql) { (rs) in
            if rs is FMResultSet {
                let newRs:FMResultSet = (rs as! FMResultSet)
                while newRs.next() {
                    let player = PlayerModel()
                    player.playerID = rs.string(forColumn: DHPlayerID)
                    player.playerNum = rs.string(forColumn: DHPlayerNum)
                    player.playerName = rs.string(forColumn: DHPlayerName)
                    player.playerTeamID = rs.string(forColumn: DHPlayerTeamID)
                    player.playerType = rs.string(forColumn: DHPlayerType)
                    player.playerMatchDataID = rs.string(forColumn: DHPlayerMatchDataID)
                    if player.playerType == "0" {
                        shoufaArr.append(player)
                    }else{
                        tibuArr.append(player)
                    }
                }
            }
        }
        dataArr = shoufaArr + tibuArr
        return dataArr
    }
}
